<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>netty-demo代码阅读</title>
    <style>
        body {
            background-color: #c7edcc
        }
    </style>
</head>
<body>
<pre>
    //通常这么写
    // Configure the server.
		bossGroup = new NioEventLoopGroup(1);//处理选择就绪事件线程
		workerGroup = new NioEventLoopGroup();//IO线程

		try {
			ServerBootstrap b = new ServerBootstrap();
			b.option(ChannelOption.SO_BACKLOG, 4096);//bocklog在linux下表示已完成队列最大长度操作128会被设置为128
			b.option(ChannelOption.SO_REUSEADDR, true);
			b.childOption(ChannelOption.SO_REUSEADDR, true);
			//设置线程池,channel工厂,初始化器等等
             //final Channel channel = channelFactory().newChannel(); NioServerSocketChannel.class.newInstance(),创建服务器channel,接受连接
            //NioServerSocketChannel完成绑定,调用javaChannel()就是ServerSocketChannel
    @Override
    protected void doBind(SocketAddress localAddress) throws Exception {
        javaChannel().socket().bind(localAddress, config.getBacklog());
    }
			b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)

			// .handler(new LoggingHandler(LogLevel.INFO))
					.childHandler(new ChannelInitializer<SocketChannel>() {
                        //当连接来的时候调用ChannelInitializer初始化该channel
                        //所以channel中的hander都是各自一份,
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                        //channelRegistered
                        //在有连接建立时会调用这里初始化添加handler,每个NioChannel都有自己的handler
                        //每次bossGroup接收到连接请求创建channel并关联workerGroup中的某一个,使用轮训策略,有某一个eventGroup(I/O Thread)处理数据读取
                        //ServerBootstrapAcceptor 处理连接请求,读取数据时将ChannelInitializer放入channelPipleline,然后通知注册,初始化channel后
                        //再从channelPipleline移除ChannelInitializer
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new HttpServerCodec());
                        p.addLast(new HttpServerOutboundHandler());
                        p.addLast(new HttpServerInboundHandler());

                        //采用另外的线程处理池
                        //p.addLast(gameExecutorGroup, new HttpServerCodec());
                        //p.addLast(gameExecutorGroup, new NettyHttpServerHandler());

    }
    });

    b.bind(HTTP_PORT).sync();
    log.error("启动Netty Http Server , 端口 :" + HTTP_PORT);



</pre>
</body>
</html>